package cn.fql.org.service.impl;

import cn.fql.basic.exception.BusinessException;
import cn.fql.basic.util.BaiduAuditUtils;
import cn.fql.basic.util.Md5Utils;
import cn.fql.basic.util.StrUtils;
import cn.fql.org.domain.Employee;
import cn.fql.org.domain.Shop;
import cn.fql.org.domain.ShopAuditLog;
import cn.fql.org.dto.ShopDto;
import cn.fql.org.mapper.EmployeeMapper;
import cn.fql.org.mapper.ShopAuditLogMapper;
import cn.fql.org.mapper.ShopMapper;
import cn.fql.org.service.IShopService;
import cn.fql.basic.service.impl.BaseServiceImpl;
import cn.fql.user.domain.Logininfo;
import cn.fql.user.mapper.LogininfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;
    @Override
    public void settlement(Shop shop) {
        //1.需要校验，前端的校验容易被禁用掉，所以后端在进行校验
        if(StringUtils.isEmpty(shop.getName())){
            throw new BusinessException("店铺的名称不能为空");
        }
        if(StringUtils.isEmpty(shop.getAddress())){
            throw new BusinessException("店铺的地址不能为空");
        }
        //校验：百度Ai智能审核
        //审核文本
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名称不合法！");
        }
        //审核图片：如果你上传了图片 但是上传的图片不合法
        if(!StringUtils.isEmpty(shop.getLogo())
        && !BaiduAuditUtils.ImgCensor(("http://123.207.27.208"+shop.getLogo()))){
            throw new BusinessException("店铺的LOGO不合法！");
        }

        //校验当前的店铺是否入住：只要名称和地址是一样的
        Shop dbshop = shopMapper.findByNameAndAddress(shop);
        if(dbshop!= null){
            throw new BusinessException("你的店铺已经入住，请直接登录！如果忘记密码，请直接联系管理员！");
        }
        //将数据添加到数据库
        Employee employee = shop.getAdmin();

        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //1.先添加t_logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0); //0是管理员
        logininfo.setDisable(false);
        logininfoMapper.save(logininfo);

        //2.再添加t_employee => 自增长id
        //先设置为禁用，审核通过并激活成功之后设置为1
        employee.setLogininfo_id(logininfo.getId());
        employee.setState(0);
        employeeMapper.save(employee);

        //2.将自增长id设置到shop对象
        shop.setAdminId(employee.getId());
        //3.再添加t_shop => 自增长id
        shopMapper.save(shop);

        //4.将shop的自增长id设置到employee对象
        employee.setShop_id(shop.getId());

        //5.修改t_employee
        employeeMapper.update(employee);
    }

    @Override
    public void pass(ShopAuditLog log) throws MessagingException {
        //1.改店铺状态：shop state 1 - 2
        shopMapper.updateState(log.getShopId(),2);
        //2.添加审核日志：
        log.setState(2);
        shopAuditLogMapper.save(log);
        //3.发送邮件：告诉店铺管理员激活邮件使用
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("928877381@qq.com");
        helper.setSubject("店铺激活邮件");
        //true - 能够再内容中编写html标签 - 会解析
        Shop shop = shopMapper.findById(log.getShopId());
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://123.207.27.208"+shop.getLogo()
                        +"' ><a href='http://localhost:8080/shop/active/"+shop.getId()+"'>点击该链接激活</a>",
                true);
        Employee employee = employeeMapper.findById(shop.getAdminId());
        //收件人
        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);
    }


    @Override
    public void reject(ShopAuditLog log) throws MessagingException {
        //1.改店铺状态：shop state 1 - 4
        shopMapper.updateState(log.getShopId(),4);

        //2.添加审核日志：
        log.setState(4);
        shopAuditLogMapper.save(log);

        //3.发送邮件：告诉店铺管理员激活邮件使用
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("928877381@qq.com");
        helper.setSubject("店铺审核失败");
        //true - 能够再内容中编写html标签 - 会解析
        Shop shop = shopMapper.findById(log.getShopId());
        helper.setText("<h1>你的店铺入驻失败，请修改信息之后重新入驻</h1>",
                true);
        Employee employee = employeeMapper.findById(shop.getAdminId());
        //收件人
        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);
    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }
}
